unit uFormReg;

interface

uses Classes, uBaseCalcForm;

type TCalcEnumFunc = procedure(const ACalcClass: TBaseCalcFormClass; AUser: pointer); stdcall;

procedure RegisterCalc(AClass: TBaseCalcFormClass);
procedure ReleaseRegisters;

function FindCalcByKey(AKey: string): TBaseCalcFormClass;
procedure EnumCalcClasses(AEnum: TCalcEnumFunc; AUser: pointer);

implementation

uses SysUtils;

type
  PCalcClassRec = ^_CALC_CLASS_REC;
  _CALC_CLASS_REC = record
    ppClass: TBaseCalcFormClass;
  end;

var
  vList: TList = nil;

procedure RegisterCalc(AClass: TBaseCalcFormClass);
var i: integer;
    pp: PCalcClassRec;
begin
  if (vList=nil) then vList := TList.Create;
  for i := 0 to vList.Count-1 do
    if PCalcClassRec(vList[i])^.ppClass = AClass then exit;

  //if (not AClass.IsDebug) or (AClass.IsDebug and keyExists('-debug')) then begin
    New(pp);
    pp^.ppClass := AClass;
    vList.Add(pp);
  //end;
end;

procedure ReleaseRegisters;
begin
  if (vList=nil) then exit;
  while (vList.Count>0) do begin
    Dispose(vList[vList.Count-1]);
    vList.Delete(vList.Count-1);
  end;
  vList.Free; vList := nil;
end;

procedure EnumCalcClasses(AEnum: TCalcEnumFunc; AUser: pointer);
var i: integer;
begin
  if (vList=nil) or (@AEnum = nil) then exit;
  for i := 0 to vList.Count-1 do
    AEnum(PCalcClassRec(vList[i])^.ppClass, AUser);
end;

function FindCalcByKey(AKey: string): TBaseCalcFormClass;
var i: integer;
begin
  Result := nil;
  if (vList=nil) then exit;
  for i := 0 to vList.Count-1 do
    if SameText(PCalcClassRec(vList[i])^.ppClass.RunKey, AKey) then begin
      Result := PCalcClassRec(vList[i])^.ppClass;
      break;
    end;
end;

end.
